<?php

class UsersAdminController extends \BaseController {

	public function __construct(){
		
		$roles=array(
				'redactor'=>4,
				'admin'=>1,
				'usuario'=>'2',
				'admin.torneos'=>3
		);
		
		//permisos
		$rolesUsers=array($roles['admin'],$roles['admin']);
		
		$this->beforeFilter('roles:'.implode($rolesUsers,'-'));
	}
	
	
	/**
	 * Display a listing of noticiasadmins
	 *
	 * @return Response
	 */
	public function index()
	{
		$users = User::all();
		
		return View::make('contents.admin.users.index', compact('users'));
	}

	/**
	 * Show the form for creating a new noticiasadmin
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('contents.admin.users.create');
	}

	/**
	 * Store a newly created noticiasadmin in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		
		$validator = Validator::make($data = Input::except('roles'), User::$rules);
		
		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}
		
		$user = new User();
		$user->fill($data);
		$user->password=Hash::make($user->password);	
		$user->save();
		
		$user->roles()->sync(Input::get('roles'));

		return Redirect::route('admin.users.index');
	}

	/**
	 * Display the specified noticiasadmin.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$user = User::findOrFail($id);

		return View::make('contents.admin.users.show', compact('user'));
	}

	/**
	 * Show the form for editing the specified noticiasadmin.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$user = User::find($id);

		return View::make('contents.admin.users.edit', compact('user'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$user = User::findOrFail($id);
		if(Input::get('password')==''){
			$inputs=Input::except('password');
		}else{
			$inputs=Input::all();
		}
		$validator = Validator::make($data = $inputs, User::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}
		
		$user->update($data);
		if(isset($inputs['password'])){
			$user->password=Hash::make($user->password);
			$user->save();
		}
		
		$user->roles()->sync(Input::get('roles'));
		
		return Redirect::route('admin.users.index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		User::destroy($id);

		return Redirect::route('admin.users.index');
	}

}